<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('includes/functions/module_functions.php');
class ot_coupon {

	function ot_coupon() {
		$this->module_key = 'MODULE_ORDER_TOTAL_COUPON';
		$this->code = 'ot_coupon';
		$this->enabled = ((MODULE_ORDER_TOTAL_COUPON_STATUS == 'true') ? true : false);
		$this->sort_order = MODULE_ORDER_TOTAL_COUPON_SORT_ORDER;
	}

	function process(&$params, &$checkout_component)
	{
		$accountId = (int)$params['accountId'];

		$cart = &$checkout_component->cart;
		$cart_prices = &$checkout_component->cart_prices;

		$coupon_discount = 0.0;
		$tax_discount = 0.0;

		$coupon_code = prepare_input($params['coupon_code']);
		if ($coupon_code != null && strlen($coupon_code) > 0)
		{
			$sqlQuery = 'select coupon_id, coupon_amount, coupon_code, coupon_type, coupon_use from ' . TABLE_COUPONS . " where coupon_status = 1 and coupon_code = '" . mysql_real_escape_string($coupon_code) . "'";
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$i = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$coupon['id'] = (int)$row['coupon_id'];
				$coupon['amount'] = $row['coupon_amount'];
				$coupon['type'] = (int)$row['coupon_type'];
				$coupon['use'] = (int)$row['coupon_use'];
				$i++;
			}
			if ($i > 0)
			{
				if ($coupon['use'] == 0 && $accountId < 1)
				{
					$cart_prices[$this->code] = array('error' => 'loginRequired', 'value' => $coupon_discount, 'title' => 'Coupon Requires Login ('. $coupon_code . '):', 'class' => $this->code);
					return;
				}
				
				$subtotal = $cart_prices['ot_subtotal']['value'];

				if ($coupon['type'] == '0') {
					$coupon_discount = $subtotal * ($coupon['amount'] / 100) * -1.0;
				}
				else {
					$coupon_discount = $coupon['amount'] * -1.0;
				}
			
				$cart_prices[$this->code] = array('value' => $coupon_discount, 'title' => 'Coupon ('. $coupon_code . '):', 'class' => $this->code);
      	
				//taxes.
				$cartTax = 0.0;
				$sz = sizeof($cart);
				for ($i=0; $i< $sz; $i++)
				{
					$cartTax += ((double)$cart[$i]['tax'] * (int)$cart[$i]['cart_qty']);
				}
				$tax_discount = 0.0;
				if ($cartTax > 0)
				{	
					$tax_percentage = $cartTax/$subtotal;

					$tax_discount = $coupon_discount * $tax_percentage;
					$cart_prices['ot_tax']['value'] += $tax_discount;
				}
			}
			else
			{
				$cart_prices[$this->code] = array('error' => 'notfound', 'value' => $coupon_discount, 'title' => 'Coupon Not Found ('. $coupon_code . '):', 'class' => $this->code);
				return;
			}
		}
	}

	function check() {
		if (!isset($this->_check)) {
			$this->_check = check_module($this->module_key);
		}
		return $this->_check;
	}

	function install() {
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Display Coupon', 'MODULE_ORDER_TOTAL_COUPON_STATUS', 'true', 'Do you want to display the coupon discount?', '6', '1','true, false', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_COUPON_SORT_ORDER', '40', 'Sort order of display.', '6', '2', now())");
	}

	function remove() {
		remove_module($this->module_key);
	}
}
?>
